/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.ui.playlist.Playlist');

goog.require('kramerius.audio.ui.PlayerComponent');

//todo:
// getNextItem(): Item - od neho ziska track a hraje
// get active item - aby vedel, ktery je dalsi

/**
 * @constructor 
 * @param {Element} container Element containing this component.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 * The button controlls some aspects of player by calling controller methods.
 * Controller may also change state of the button.
 */

kramerius.audio.ui.playlist.Playlist = function(container, controller){
    kramerius.audio.ui.PlayerComponent.call(this, container, controller);
    this.listEl_ = this.createListElement_();
    this.footerEl_ = this.createFooterElement_();
    this.rootEl_ = this.createDom_(this.listEl_, this.footerEl_);
    this.items_ = [];
    
};
goog.inherits(kramerius.audio.ui.playlist.Playlist, kramerius.audio.ui.PlayerComponent);

kramerius.audio.ui.playlist.Playlist.prototype.createListElement_ = function(){
    return goog.dom.createDom('div',{
        'class':'playlist-list'    
    });
}

kramerius.audio.ui.playlist.Playlist.prototype.createFooterElement_ = function(){
    var playlist = this;
    var footer = goog.dom.createDom('div',{
        'class':'playlist-footer'
    });
    //var footer = goog.dom.getElement('audio.playlist.buttons');
    
    
    //button "move up active item"
    var upEl = goog.dom.createDom('span',{
        'class':'playlist-item-up-enabled'    
    });
    goog.events.listen(upEl, goog.events.EventType.CLICK,
        function (e) {
            var selectedItem = playlist.getSelectedItem();
            if (selectedItem != undefined){
                selectedItem.moveUpInPlaylist();
            }else{
                var activeItem = playlist.getActiveItem();
                if (activeItem != undefined){
                    activeItem.moveUpInPlaylist();  
                }
            }
        });
    footer.appendChild(upEl);
    
    
    //button "move down active item"
    var downEl = goog.dom.createDom('span',{
        'class':'playlist-item-down-enabled'    
    });
    goog.events.listen(downEl, goog.events.EventType.CLICK,
        function (e) {
            var selectedItem = playlist.getSelectedItem();
            if (selectedItem != undefined){
                selectedItem.moveDownInPlaylist();
            } else{
                var activeItem = playlist.getActiveItem();
                if (activeItem != undefined){
                    activeItem.moveDownInPlaylist();  
                }
            }
        });
    footer.appendChild(downEl);
    
    return footer;
}


kramerius.audio.ui.playlist.Playlist.prototype.createDom_ = function(listEl, footerEl){
    //    var rootElement = goog.dom.createDom('div',{
    //        'class':'playlist'    
    //    });
    //    rootElement.appendChild(listEl);
    //    rootElement.appendChild(footerEl);
    //    this.container_.appendChild(rootElement);

    this.container_.appendChild(listEl);
    this.container_.appendChild(footerEl);
    return rootElement;
}

kramerius.audio.ui.playlist.Playlist.prototype.getListElement = function(){
    return this.listEl_;
}


kramerius.audio.ui.playlist.Playlist.prototype.getSelectedItem = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        if (item.statusIsSelected()){
            return item;
        }
    }
    return undefined;
}

kramerius.audio.ui.playlist.Playlist.prototype.getActiveItem = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        if (item.statusIsActive()){
            return item;
        }
    }
    return undefined;
}


kramerius.audio.ui.playlist.Playlist.prototype.addItem = function(item){
    this.items_[this.items_.length] = item; 
}


kramerius.audio.ui.playlist.Playlist.prototype.playItem = function(itemToPlay){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //    console.log(i);
        item.setStatusNormal();
    }
    itemToPlay.setStatusActive();
    console.log("playing " + itemToPlay.getTrack().getTitle() + " - actual playing not implemented yet");
    //TODO: actually play the item
    this.controller_.setTrack(itemToPlay.getTrack());
    this.controller_.play();
}

kramerius.audio.ui.playlist.Playlist.prototype.selectItem = function(itemToPlay){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //  console.log(i);
        if (item.statusIsSelected())
            item.setStatusNormal();
    }
    if (itemToPlay.statusIsNormal()){
        itemToPlay.setStatusSelected();
    }    
}


kramerius.audio.ui.playlist.Playlist.prototype.getNextItemToPlay = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //  console.log(i);
        if (item.statusIsActive() && (i+1)!=this.items_.length){
            return this.items_[i+1];
        }
    }
    return null;
}













